﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using cfg = System.Configuration;
using GoldPrinter;
using WeifenLuo.WinFormsUI.Docking;
namespace BYManageInfoSystem
{
    public partial class Frm_SeaGateCompRecvP : DockContent
    {
        private DGBY.BLL.BGHRecvPeoTelTab m_bllBGHRecvPeoTelTab = new DGBY.BLL.BGHRecvPeoTelTab();
        private DGBY.BLL.BGHDataTab m_bllBGHDataTab = new DGBY.BLL.BGHDataTab();
        private DataSet m_ds = new DataSet();
        private int m_nSelectBGHRecvPeoTelTabID = 0;
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];


        public Frm_SeaGateCompRecvP()
        {
            InitializeComponent();
        }

        private void Frm_SeaGateCompRecvP_Load(object sender, EventArgs e)
        {
            DataSet ds = m_bllBGHDataTab.GetList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxChooseSeaGateComp.DataSource = ds.Tables[0];
                this.comboBoxChooseSeaGateComp.DisplayMember = "报关行名称";
                this.comboBoxChooseSeaGateComp.ValueMember = "BGHDataTabID";
                this.comboBoxChooseSeaGateComp.SelectedIndex = 0;
            }
            if (Frm_Main.m_strLoginUser == "管理员")
            {
                this.buttonDel.Visible = true;
            }
        }

        private void comboBoxChooseSeaGateComp_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                string strCompID = this.comboBoxChooseSeaGateComp.SelectedValue.ToString();
                dataGridViewSeaGateCompPersonAndPhoneList.DataSource = null;
                m_ds = m_bllBGHRecvPeoTelTab.GetRecvPListByCompID(strCompID);
                if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
                {
                    dataGridViewSeaGateCompPersonAndPhoneList.DataSource = m_ds.Tables[0];
                    dataGridViewSeaGateCompPersonAndPhoneList.Columns[0].Visible = false;
                }
            }
            catch { }
        }

        private void ShowAllRecordInDataGridView()
        {
            dataGridViewSeaGateCompPersonAndPhoneList.DataSource = null;
            m_ds = m_bllBGHRecvPeoTelTab.GetAllRecvPList();
            if (m_ds.Tables.Count > 0 && m_ds.Tables[0].Rows.Count > 0)
            {
                dataGridViewSeaGateCompPersonAndPhoneList.DataSource = m_ds.Tables[0];
                dataGridViewSeaGateCompPersonAndPhoneList.Columns[0].Visible = false;
            }
        }

        private void buttonShowAllRecord_Click(object sender, EventArgs e)
        {
            ShowAllRecordInDataGridView();
        }



        private void cellclickBGHRecvPeoTelTab()
        {
            DGBY.Model.BGHRecvPeoTelTab modelBGHRecvPeoTelTab = m_bllBGHRecvPeoTelTab.GetModel(m_nSelectBGHRecvPeoTelTabID);
            if (modelBGHRecvPeoTelTab != null)
            {
                if (modelBGHRecvPeoTelTab.BGHDataTabID != null)
                    this.comboBoxChooseSeaGateComp.SelectedValue = modelBGHRecvPeoTelTab.BGHDataTabID;
                this.textBoxConPerson.Text = modelBGHRecvPeoTelTab.BGHRecvPeoName;
                this.textBoxConPhone.Text = modelBGHRecvPeoTelTab.BGHRecvPeoTel;
            }
            else
            {
                this.textBoxConPerson.Text = "";
                this.textBoxConPhone.Text = "";
            }
        }



        private void buttonModify_Click(object sender, EventArgs e)
        {
            try
            {
                if (m_bllBGHRecvPeoTelTab.GetModel(m_nSelectBGHRecvPeoTelTabID) == null)
                {
                    MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }

                DGBY.Model.BGHRecvPeoTelTab modelBGHRecvPeoTelTab = m_bllBGHRecvPeoTelTab.GetModel(m_nSelectBGHRecvPeoTelTabID);
                modelBGHRecvPeoTelTab.BGHRecvPeoName = this.textBoxConPerson.Text;
                modelBGHRecvPeoTelTab.BGHRecvPeoTel = this.textBoxConPhone.Text;
                //modelBGHRecvPeoTelTab.BGHRecvPeoTelTabID = m_nSelectBGHRecvPeoTelTabID;

                if (this.comboBoxChooseSeaGateComp.SelectedValue != null)
                    modelBGHRecvPeoTelTab.BGHDataTabID = Convert.ToInt32(this.comboBoxChooseSeaGateComp.SelectedValue.ToString(), 10);

                m_bllBGHRecvPeoTelTab.Update(modelBGHRecvPeoTelTab);

                Frm_Main.Log2DB(this.Text, "修改", this.comboBoxChooseSeaGateComp.Text + "(" + modelBGHRecvPeoTelTab.BGHRecvPeoName + ")");

                ShowAllRecordInDataGridView();
                MessageBox.Show("数据更新成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        private void buttonDel_Click(object sender, EventArgs e)
        {
            if (m_bllBGHRecvPeoTelTab.GetModel(m_nSelectBGHRecvPeoTelTabID) == null)
            {
                MessageBox.Show("数据库不存在该条记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("确实要删除该记录么，删除该记录可能会连带删除其他表记录!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (dr == DialogResult.Cancel)
            {
                return;
            }
            m_bllBGHRecvPeoTelTab.Delete(m_nSelectBGHRecvPeoTelTabID);
            ShowAllRecordInDataGridView();

            MessageBox.Show("数据删除成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            cellclickBGHRecvPeoTelTab();
        }

        private void buttonAdd_Click_1(object sender, EventArgs e)
        {
            try
            {
                DGBY.Model.BGHRecvPeoTelTab modelBGHRecvPeoTelTab = new DGBY.Model.BGHRecvPeoTelTab();
                modelBGHRecvPeoTelTab.BGHRecvPeoName = this.textBoxConPerson.Text;
                modelBGHRecvPeoTelTab.BGHRecvPeoTel = this.textBoxConPhone.Text;

                if (this.comboBoxChooseSeaGateComp.SelectedValue != null)
                    modelBGHRecvPeoTelTab.BGHDataTabID = Convert.ToInt32(this.comboBoxChooseSeaGateComp.SelectedValue.ToString(), 10);

                int nResult = m_bllBGHRecvPeoTelTab.Add(modelBGHRecvPeoTelTab);

                if (nResult != 1)
                {
                    Frm_Main.Log2DB(this.Text, "增加", this.comboBoxChooseSeaGateComp.Text + "(" + modelBGHRecvPeoTelTab.BGHRecvPeoName + ")");

                    m_nSelectBGHRecvPeoTelTabID = m_bllBGHRecvPeoTelTab.GetMaxId() - 1;
                    ShowAllRecordInDataGridView();
                    //MessageBox.Show("数据添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }

        private void dataGridViewSeaGateCompPersonAndPhoneList_CellMouseClick_1(object sender, DataGridViewCellMouseEventArgs e)
        {
            string strBGHRecvPeoTelTabID = this.dataGridViewSeaGateCompPersonAndPhoneList.Rows[this.dataGridViewSeaGateCompPersonAndPhoneList.CurrentCell.RowIndex].Cells[0].Value.ToString();
            m_nSelectBGHRecvPeoTelTabID = Convert.ToInt32(strBGHRecvPeoTelTabID, 10);
            cellclickBGHRecvPeoTelTab();
        }

        private void dataGridViewSeaGateCompPersonAndPhoneList_RowPostPaint_1(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            {
                using (SolidBrush b = new SolidBrush(dataGridViewSeaGateCompPersonAndPhoneList.RowHeadersDefaultCellStyle.ForeColor))
                {
                    e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), e.InheritedRowStyle.Font, b, e.RowBounds.Location.X + 10, e.RowBounds.Location.Y + 4);
                }
            }
        }

        
    }
}